export default {
  data() {
    return {
      img_api: process.env.VUE_APP_BASEURL_IMG_API,
      urlParams: {},
      parseStyle: {
        // 字符串的形式
        h1: "margin-bottom: 0.5em",
        h2: "margin-bottom: 0.6em",
        h3: "margin-bottom: 0.7em",
        h4: "margin-bottom: 0.8em",
        h5: "margin-bottom: 0.9em",
        p: "margin-bottom: 1em",
      },
    };
  },
  computed: {
    token() {
      return this.$store.state.userInfo.token;
    },
    userInfo() {
      return this.$store.state.userInfo.userInfo;
    },
  },
  onShow() {
    this.urlParams = this.$store.state.urlParams;
    this.$store.commit("setUrlParams", {});
  },
  methods: {
    // 页面跳转 /图片预览
    toURL(
      url,
      params = {},
      { type = "url", subType = "url", isAuth = false } = {}
    ) {
      if (isAuth && !this.$store.state.userInfo.token) {
        uni.navigateTo({
          url: "/pages/login/index",
        });
        return;
      }

      let paramsString = "";
      if (Object.keys(params).length > 0) {
        this.$store.commit("setUrlParams", params);

        for (const paramsKey in params) {
          if (!paramsString) {
            paramsString += "?";
          } else {
            paramsString += "&";
          }
          paramsString += `${paramsKey}=${params[paramsKey]}`;
        }
      }

      switch (type) {
        case "back": {
          uni.navigateBack({
            delta: 1,
          });
          break;
        }
        case "toURL": {
          // 跳转操作，找到页面栈中的目标，出栈后面的页面
          let pages = getCurrentPages();
          let index = pages.findIndex((page) => {
            return url.includes(page.route);
          });
          if (index !== -1) {
            uni.navigateBack({
              delta: pages.length - index - 1,
            });
          } else {
            this.toURL(url, params, { type: subType });
          }
          break;
        }
        case "redirect": {
          if (!url) return;
          uni.redirectTo({
            url: `${url}${paramsString}`,
          });
          break;
        }
        case "reLaunch": {
          if (!url) return;
          uni.reLaunch({
            url: `${url}${paramsString}`,
          });
          break;
        }
        case "switch": {
          if (!url) return;
          console.log(`${url}${paramsString}`);
          uni.switchTab({
            url: `${url}${paramsString}`,
          });
          break;
        }
        case "top": {
          uni.pageScrollTo({
            scrollTop: 0,
          });
          break;
        }
        case "copy": {
          uni.setClipboardData({
            data: url,
            success(res) {
              wx.getClipboardData({
                success(res) {
                  uni.showToast({
                    title: "已复制到剪贴板！",
                    icon: "none",
                  });
                },
              });
            },
          });
          break;
        }
        case "phone": {
          //
          if (!url) {
            uni.showToast({
              title: "暂无联系电话",
              icon: "none",
            });
            return;
          }
          if (uni.$u.os() === "android") {
            uni.showModal({
              title: "提示",
              content: `确认拨打${url}？ `,
              success(res) {
                if (res.confirm) {
                  uni.makePhoneCall({
                    phoneNumber: url,
                  });
                }
              },
            });
            return;
          }
          uni.makePhoneCall({
            phoneNumber: url,
          });
          break;
        }
        default:
          if (!url) return;
          uni.navigateTo({
            url: `${url}${paramsString}`,
          });
      }
    },

    /**
     * 获取父页面
     * @param {*} deep  1=当前页 2=父页 3=父父页
     */
    getPrevPage(deep = 2) {
      let pages = getCurrentPages();
      return pages[pages.length - deep]; // 上一个页面
    },
  },
};
